package com.google.code.swingonfire.showcase.utils;

import java.util.EventListener;

import javax.swing.JSlider;

public class DoubleSliderParameterDefinition implements ParameterDefinition<JSlider, Double>
{

    private final IntSliderParameterDefinition sliderParameterDefinition;

    public DoubleSliderParameterDefinition(String name, double minimum, double value, double maximum,
        double majorTicks, double minorTicks)
    {
        this(name, minimum, value, maximum, majorTicks, minorTicks, "%,.2f");
    }

    public DoubleSliderParameterDefinition(String name, double minimum, double value, double maximum,
        double majorTicks, double minorTicks, final String format)
    {
        super();

        sliderParameterDefinition =
            new IntSliderParameterDefinition(name, (int) (minimum * 100), (int) (value * 100), (int) (maximum * 100),
                (int) (majorTicks * 100), (int) (minorTicks * 100))
            {

                @Override
                protected String format(int i)
                {
                    return String.format(format, (double) i / 100);
                }
            };
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName()
    {
        return sliderParameterDefinition.getName();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Class<? extends Double> getParameterType()
    {
        return Double.class;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JSlider createComponent(EventListener eventListener)
    {
        return sliderParameterDefinition.createComponent(eventListener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Double getValue(JSlider component)
    {
        return (double) component.getValue() / 100;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setValue(JSlider component, Double value)
    {
        component.setValue((int) (value.doubleValue() * 100));
    }

}
